package cn.kmsoft.stgeometry.util;

import cn.kmsoft.wololo.object.GeoJson;
import cn.kmsoft.wololo.object.GeoJsonFeature;
import cn.kmsoft.wololo.object.query.AnalysisType;
import cn.kmsoft.wololo.object.query.GeoJsonQuery;
import cn.kmsoft.wololo.object.query.GeoJsonQuery2;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * GeoJsonQueryUtil  工具
 * 实现了GeoJsonQuery与Shp、GeoJson对象的相互转换
 * 
 */
public class GeoJsonQueryUtil {

    /**
     * Shp文件转换为 GeoJsonQuery对象
     *
     * @param shppath
     * @param wkid
     * @return
     */
    public static GeoJsonQuery shp2GeoQuery(String shppath, int wkid) {
        //1-shp转GeoJson
        String geoJSonStr = Shapefile2GeoJson.shp2GeoJsonStr(shppath);
        //2-geoJSonStr转GeoJsonQuery
        GeoJsonQuery geoJsonQuery = geoJson2GeoQuery(geoJSonStr, wkid);
        return geoJsonQuery;
    }
    public static GeoJsonQuery shp2GeoQuery(String shppath, int wkid, AnalysisType analysisType) {
        //1-shp转GeoJson
        String geoJSonStr = Shapefile2GeoJson.shp2GeoJsonStr(shppath);
        //2-geoJSonStr转GeoJsonQuery
        GeoJsonQuery geoJsonQuery = geoJson2GeoQuery(geoJSonStr, wkid, analysisType);
        return geoJsonQuery;
    }
    public static String shp2GeoQueryStr(String shppath, int wkid) {
        //1-shp转GeoJson
        String geoJSonStr = Shapefile2GeoJson.shp2GeoJsonStr(shppath);
        //2-geoJSonStr转GeoJsonQuery
        GeoJsonQuery geoJsonQuery = geoJson2GeoQuery(geoJSonStr, wkid);
        //3-输出jsonStr
        String geoJsonQueryStr = JSON.toJSONString(geoJsonQuery);

        return geoJsonQueryStr;
    }
    public static GeoJsonQuery2 shp2GeoQuery2(String shppath, int wkid) {
        //1-shp转GeoJson对象
        GeoJson geoJson = Shapefile2GeoJson.shp2GeoJson(shppath);
        //2-geoJSonStr转GeoJsonQuery
        GeoJsonQuery2 geoJsonQuery = geoJson2GeoQuery(geoJson, wkid);
        return geoJsonQuery;
    }
    public static GeoJsonQuery2 shp2GeoQuery2(String shppath, int wkid, AnalysisType analysisType) {
        //1-shp转GeoJson对象
        GeoJson geoJson = Shapefile2GeoJson.shp2GeoJson(shppath);
        //2-geoJSonStr转GeoJsonQuery
        GeoJsonQuery2 geoJsonQuery = geoJson2GeoQuery(geoJson, wkid, analysisType);
        return geoJsonQuery;
    }

    /**
     * GeoJson字符串转GeoJsonQuery对象
     * 设置默认的wkid=4324，查询类型为INTERSECT
     * @param geoJsonStr
     * @return
     */
    public static GeoJsonQuery geoJson2GeoQuery(String geoJsonStr) {
        //GeoJson geoJson = JSON.parseObject(geoJsonStr, GeoJson.class);

        GeoJsonQuery geoQuery = new GeoJsonQuery();
        geoQuery.setDigits(4);
        geoQuery.setReturnEllipsoidalArea(true);
        geoQuery.setType(AnalysisType.INTERSECT);
        geoQuery.setWkid(4324);
        geoQuery.setGeoJson(geoJsonStr);

        //String geoqueryStr = JSON.toJSONString(geoQuery);
        return geoQuery;
    }
    public static GeoJsonQuery geoJson2GeoQuery(String geoJsonStr, int wkid) {
        //GeoJson geoJson = JSON.parseObject(geoJsonStr, GeoJson.class);

        GeoJsonQuery geoQuery = new GeoJsonQuery();
        geoQuery.setDigits(4);
        geoQuery.setReturnEllipsoidalArea(true);
        geoQuery.setType(AnalysisType.INTERSECT);
        geoQuery.setWkid(wkid);
        geoQuery.setGeoJson(geoJsonStr);

        //String geoqueryStr = JSON.toJSONString(geoQuery);

        return geoQuery;
    }
    public static GeoJsonQuery geoJson2GeoQuery(String geoJsonStr, int wkid, AnalysisType analysisType) {
        //GeoJson geoJson = JSON.parseObject(geoJsonStr, GeoJson.class);

        GeoJsonQuery geoQuery = new GeoJsonQuery();
        geoQuery.setDigits(4);
        geoQuery.setReturnEllipsoidalArea(true);
        geoQuery.setType(analysisType);
        geoQuery.setWkid(wkid);
        geoQuery.setGeoJson(geoJsonStr);

        //String geoqueryStr = JSON.toJSONString(geoQuery);

        return geoQuery;
    }
    public static GeoJsonQuery2 geoJson2GeoQuery(GeoJson geoJson, int wkid) {
        //GeoJson geoJson = JSON.parseObject(geoJsonStr, GeoJson.class);

        GeoJsonQuery2 geoQuery = new GeoJsonQuery2();
        geoQuery.setDigits(4);
        geoQuery.setReturnEllipsoidalArea(true);
        geoQuery.setType(AnalysisType.INTERSECT);
        geoQuery.setWkid(wkid);
        geoQuery.setGeoJson(geoJson);

        //String geoqueryStr = JSON.toJSONString(geoQuery);

        return geoQuery;
    }
    public static GeoJsonQuery2 geoJson2GeoQuery(GeoJson geoJson, int wkid, AnalysisType analysisType) {
        //GeoJson geoJson = JSON.parseObject(geoJsonStr, GeoJson.class);

        GeoJsonQuery2 geoQuery = new GeoJsonQuery2();
        geoQuery.setDigits(4);
        geoQuery.setReturnEllipsoidalArea(true);
        geoQuery.setType(analysisType);
        geoQuery.setWkid(wkid);
        geoQuery.setGeoJson(geoJson);

        //String geoqueryStr = JSON.toJSONString(geoQuery);

        return geoQuery;
    }

    /**
     * GeoJsonQuery字符串转换为shp文件
     *
     * @param geoQueryStr
     * @param outshpfile  输出shp路径
     * @param wktEspg  投影坐标
     * @param encoding shp文件编码
     */
    public static void geoJsonQuery2Shp(String geoQueryStr, String outshpfile, String wktEspg, String encoding) {
        //字符串转GeoJsonQuery对象
        GeoJsonQuery geoJsonQuery = JSON.parseObject(geoQueryStr, GeoJsonQuery.class);
        System.out.println("--->geoJsonQuery: " + JSON.toJSONString(geoJsonQuery));
        //geoJson的字符串
        String geoJsonStr = geoJsonQuery.getGeoJson();

        JSONObject jsonObject = JSON.parseObject(geoJsonStr);

        GeoJson geoJson = JSON.parseObject(geoJsonStr, GeoJson.class);
        List<GeoJsonFeature> feaList = geoJson.getFeatures();
        for (int i = 0; i < feaList.size(); i++) {
            GeoJsonFeature geoFea = feaList.get(i);
            System.out.println();
            System.out.println("--->GeoJsonFeature: " + JSON.toJSONString(geoFea));

            //属性信息
            Map<String, Object> props = geoFea.getProperties();
            if (props == null) {
                System.out.println("--->该数据无属性信息");
                continue;
            }
            Set<String> keySet = props.keySet();
            Iterator<String> keyIter = keySet.iterator();
            while (keyIter.hasNext()) {
                String key = keyIter.next();
                String val = props.get(key) != null ? props.get(key).toString() : "";
                System.out.println(key + ": " + val);
            }
        }

        try {
            GeoJson2Shapefile.geoJson2Shp(wktEspg, jsonObject, outshpfile, encoding);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
